﻿using System;
using System.IO;
using System.Text;

namespace SomeKit.Serialization
{
    public static partial class SerializationHelper
    {
        public static string Serialize<T>(this T obj, Action<Stream, T> objectWriter)
        {
            using (var memoryStream = new MemoryStream())
            {
                obj.SerializeToStream(memoryStream, objectWriter);
                memoryStream.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(memoryStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
                return content;
            }
        }

        public static string SerializeXml<T>(this T obj)
        {
            return obj.Serialize<T>(WriteObjectAsXml<T>);
        }

        public static string SerializeJson<T>(this T obj)
        {
            return obj.Serialize<T>(WriteObjectAsJson<T>);
        }
    }
}
